package com.ship.auth.bean;

import com.ship.common.core.utils.Constants;
import com.ship.common.redis.configure.RedisRepository;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;

@Configuration
@MapperScan("com.ship.auth.mapper")
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

        @Bean
    public RedisTemplate<String, Object> bxRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(Constants.STRING_SERIALIZER);
        redisTemplate.setHashKeySerializer(Constants.STRING_SERIALIZER);
        redisTemplate.setValueSerializer(Constants.OBJECT_SERIALIZER);
        redisTemplate.setHashValueSerializer(Constants.OBJECT_SERIALIZER);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean
    public RedisRepository redisRepository(RedisTemplate<String, Object> bxRedisTemplate) {
        return new RedisRepository(bxRedisTemplate);
    }
}
